package com.esm.pdawms.activity;

import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.res.AssetFileDescriptor;
import android.content.res.AssetManager;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.os.Vibrator;
import android.view.KeyEvent;
import android.view.View;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.esm.pdawms.R;
import com.esm.pdawms.adapter.DemoAdapter;
import com.esm.pdawms.database.manager.SystemCenter;
import com.esm.pdawms.model.Hprkcode;
import com.esm.pdawms.util.HttpRequest;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import zuo.biao.library.base.BaseActivity;
import zuo.biao.library.interfaces.OnHttpResponseListener;
import zuo.biao.library.model.Entry;
import zuo.biao.library.util.JSON;

public class HpbhActivity extends BaseActivity implements View.OnClickListener, MyReceiver.Message {
    private static final String TAG = "HpbhActivity";

    public static Intent createIntent(Context context) {
        return new Intent(context, HpbhActivity.class);
    }

    MyReceiver myReceiver;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_hpbh);

        initView();
        initData();
        initEvent();

        myReceiver = new MyReceiver();
        myReceiver.message = this;
        IntentFilter intentFilter = new IntentFilter();
        intentFilter.addAction("android.intent.ACTION_DECODE_DATA");
        intentFilter.addAction("com.scanner.broadcast");
        registerReceiver(myReceiver, intentFilter);

        edit_hkcode.setText(SystemCenter.getInstance().esmcode);
        scanHpcode(SystemCenter.getInstance().esmcode);
    }

    public static final int BAOJING = 1819;//报警

    public Handler mHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            if (BAOJING == msg.what) {
                baojing();
            }
        }
    };

    AssetFileDescriptor afd = null;
    AssetManager am = null;
    //错误扫码报警
    void baojing() {
        Vibrator vibrator = (Vibrator) getSystemService(VIBRATOR_SERVICE);
        //这里示例的代码是使用点击按钮的时候可以实现震动，在按钮的点击事件的函数中添加这样几句话
        /*这里使用的是一个长整型数组，数组的a[0]表示静止的时间，a[1]代表的是震动的时间，然后数组的a[2]表示静止的时间，
         * a[3]代表的是震动的时间……依次类推下去，然后这里的代码有一点小小的改变
         */
        long[] patter = {50, 1000};
        vibrator.vibrate(patter, -1);

        //播放声音
        if (am == null) {
            am = getAssets();
            try {
                afd = am.openFd("14184.mp3");
            } catch (IOException e) {

            }
        }
        initMediaPlayer();
    }

    private final MediaPlayer mediaPlayer = new MediaPlayer();
    private void initMediaPlayer() {
        try {
            mediaPlayer.reset();
            mediaPlayer.setDataSource(afd.getFileDescriptor(), afd.getStartOffset(), afd.getLength());
            mediaPlayer.prepare();
            mediaPlayer.start();

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private TextView tv_dqzkinfo;

    private EditText edit_hkcode;
    private EditText edit_hpname;
    private EditText edit_hpcode;
    private EditText edit_hpgys;
    private EditText edit_pici;
    private EditText edit_ynum;
    private EditText edit_state;
    private EditText edit_cwbh;
    private EditText edit_cwtype;

    @Override
    public void initView() {
        tv_dqzkinfo = (TextView) findViewById(R.id.tv_dqzkinfo);
        edit_hkcode = (EditText) findViewById(R.id.edit_hkcode);
        edit_hpname = (EditText) findViewById(R.id.edit_hpname);
        edit_hpcode = (EditText) findViewById(R.id.edit_hpcode);
        edit_pici = (EditText) findViewById(R.id.edit_pici);
        edit_hpgys = (EditText) findViewById(R.id.edit_hpgys);
        edit_ynum = (EditText) findViewById(R.id.edit_ynum);
        edit_state = (EditText) findViewById(R.id.edit_state);
        edit_cwbh = (EditText) findViewById(R.id.edit_cwbh);
        edit_cwtype = (EditText) findViewById(R.id.edit_cwtype);

        reset();
    }

    private void reset() {
        edit_hpname.setText("");
        edit_hpcode.setText("");
        edit_pici.setText("");
        edit_hpgys.setText("");
        edit_ynum.setText("");
        edit_state.setText("");
        edit_cwbh.setText("");
        edit_cwtype.setText("");
        _hkcode = "";
    }

    @Override
    public void initData() {

    }

    @Override
    public void initEvent() {
        findViewById(R.id.tv_ok).setOnClickListener(this);
        findViewById(R.id.tv_ng).setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        float num = 0;
        try {
            num = Float.parseFloat(edit_ynum.getText().toString());
        } catch (Exception e1) {
            showShortToast("请输入数量！");
            return;
        }

        switch (v.getId()) {
            case R.id.tv_ok:
                onUpdatenum(num);
                break;
            case R.id.tv_ng:
                onUpdatenum(0);
                break;
            default:
                break;
        }
    }

    private void onUpdatenum(float num) {
        showProgressDialog(R.string.loading);
        try {
            HttpRequest.updatehknum(_hkcode, num, 0, new OnHttpResponseListener() {
                @Override
                public void onHttpResponse(int requestCode, String resultJson, Exception e) {
                    dismissProgressDialog();

                    JSONObject jsonObject = JSON.parseObject(resultJson);
                    int code = 0;
                    if (jsonObject != null) {
                        code = jsonObject.getInteger("code");
                    } else {
                        showShortToast(getString(R.string.wlwlj));
                        tv_dqzkinfo.setText(getString(R.string.wlwlj));
                        return;
                    }

                    if (code == 1) {
                        finish();
                    } else {
                        mHandler.sendEmptyMessage(BAOJING);
                        String msg = jsonObject.getString("msg");
                        tv_dqzkinfo.setText(msg);
                        showShortToast(msg);
                    }
                }
            });
        } catch (Exception e1) {
            showShortToast(getString(R.string.wlwlj));
            dismissProgressDialog();
        }
    }

    @Override
    public boolean onKeyUp(int keyCode, KeyEvent event) {
        switch (keyCode) {
            case 514:
                break;
            case 782:
                //扫码按键
                break;
        }
        return super.onKeyUp(keyCode, event);
    }

    String _hkcode = "";

    @Override
    public void getMsg(String str) {
        //通过实现MyReceiver.Message接口可以在这里对MyReceiver中的数据进行处理
        if (str.isEmpty()) {
            return;
        }
        showShortToast(str);
        if (_hkcode.compareToIgnoreCase(str) == 0) {
            return;
        } else {
            //扫货品码
            edit_hkcode.setText(str);
            scanHpcode(str);
        }
    }
    //扫货品编号
    void scanHpcode(String hkcode) {
        showProgressDialog(R.string.loading);
        try {
            HttpRequest.hprkcodedetail(hkcode, 0, new OnHttpResponseListener() {
                @Override
                public void onHttpResponse(int requestCode, String resultJson, Exception e) {
                    dismissProgressDialog();

                    JSONObject jsonObject = JSON.parseObject(resultJson);
                    int code = 0;
                    if (jsonObject != null) {
                        code = jsonObject.getInteger("code");
                    } else {
                        showShortToast(getString(R.string.wlwlj));
                        tv_dqzkinfo.setText(getString(R.string.wlwlj));
                        return;
                    }

                    if (code == 1) {
                        _hkcode = hkcode;
                        String data = jsonObject.getString("data");
                        JSONObject jsonObject2 = JSON.parseObject(data);
                        String hpbh = jsonObject2.getString("hpbh");
                        String hpname = jsonObject2.getString("hpname");
                        String hpcode = jsonObject2.getString("hpcode");
                        String hpgys = jsonObject2.getString("hpgys");
                        String ph = jsonObject2.getString("ph");
                        int num = jsonObject2.getInteger("num");
                        String state = jsonObject2.getString("state");
                        String cwbh = jsonObject2.getString("cwbh");
                        String cwtype = jsonObject2.getString("cwtype");
                        edit_hpname.setText(hpname);
                        edit_pici.setText(ph);
                        edit_hkcode.setText(hpbh);
                        edit_hpcode.setText(hpcode);
                        edit_pici.setText(ph);
                        edit_hpgys.setText(hpgys);
                        if (state.compareToIgnoreCase("0") == 0) {
                            edit_state.setText("待入库");
                        } else if (state.compareToIgnoreCase("1") == 0) {
                            edit_state.setText("已入库");
                        } else {
                            edit_state.setText("已出库");
                        }
                        edit_ynum.setText(num + "");
                        edit_cwbh.setText(cwbh);
                        edit_cwtype.setText(cwtype);
                        tv_dqzkinfo.setText("正确的货品小标！");
                        findViewById(R.id.ll_xbinfo).setVisibility(View.VISIBLE);
                    } else {
                        mHandler.sendEmptyMessage(BAOJING);
                        String msg = jsonObject.getString("msg");
                        tv_dqzkinfo.setText(msg);
                        showShortToast(msg);
                        reset();
                    }

                }
            });
        } catch (Exception e1) {
            showShortToast(getString(R.string.wlwlj));
            tv_dqzkinfo.setText(getString(R.string.wlwlj));
            dismissProgressDialog();
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        unregisterReceiver(myReceiver);
    }

}